package com.next

import grails.validation.ValidationException
import static org.springframework.http.HttpStatus.*
import org.springframework.security.access.annotation.Secured

@Secured(['ROLE_ADMINISTRATOR'])
class OptionInstanceController
{

    OptionInstanceService optionInstanceService

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max)
    {
        params.max = Math.min(max ?: 10, 100)
        respond optionInstanceService.list(params), model: [optionInstanceCount: optionInstanceService.count()]
    }

    def show(Long id)
    {
        respond optionInstanceService.get(id)
    }

    def create()
    {
        respond new OptionInstance(params)
    }

    def save(OptionInstance optionInstance)
    {
        if (optionInstance == null)
        {
            notFound()
            return
        }

        try
        {
            optionInstanceService.save(optionInstance)
        }
        catch (ValidationException e)
        {
            respond optionInstance.errors, view: 'create'
            return
        }

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'optionInstance.label', default: 'OptionInstance'), optionInstance.id])
                redirect optionInstance
            }
            '*' { respond optionInstance, [status: CREATED] }
        }
    }

    def edit(Long id)
    {
        respond optionInstanceService.get(id)
    }

    def update(OptionInstance optionInstance)
    {
        if (optionInstance == null)
        {
            notFound()
            return
        }

        try
        {
            optionInstanceService.save(optionInstance)
        }
        catch (ValidationException e)
        {
            respond optionInstance.errors, view: 'edit'
            return
        }

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'optionInstance.label', default: 'OptionInstance'), optionInstance.id])
                redirect optionInstance
            }
            '*' { respond optionInstance, [status: OK] }
        }
    }

    def delete(Long id)
    {
        if (id == null)
        {
            notFound()
            return
        }

        optionInstanceService.delete(id)

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'optionInstance.label', default: 'OptionInstance'), id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NO_CONTENT }
        }
    }

    protected void notFound()
    {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'optionInstance.label', default: 'OptionInstance'), params.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NOT_FOUND }
        }
    }
}
